package main.java.tmi.returnProduct.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.service.UserVO;
import main.java.tmi.returnProduct.service.ReturnProductService;
import main.java.tmi.returncause.service.ReturncauseService;
import main.java.tmi.returncause.service.ReturncauseVO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class ReturnProductController {
	
    @Resource(name = "returnProductServiceImpl")
    private ReturnProductService returnProductService;
    
    @Resource(name = "returncauseServiceImpl")
    private ReturncauseService returncauseService;
    
    /**
     * The Constructor
     */
    public ReturnProductController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/returnproduct")
    public String viewList(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============returnProduct start==================");
        System.out.println("====================================================");
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("paging", "0");
		paramMap.put("listScale", "100000");
        List<ReturncauseVO> listReturnCause = returncauseService.getReturncauseView(paramMap);
        UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
		model.addAttribute("roleId",userInfo.getRoleId());
        model.addAttribute("listReturnCause", listReturnCause);
        model.addAttribute("returnDate",param.get("returnDate"));
        model.addAttribute("goodsCode",param.get("goodsCode"));
        model.addAttribute("batchNo",param.get("batchNo"));
        model.addAttribute("goodsName",param.get("goodsName"));
        return "/tmi/returnproduct/returnproduct";
    } 

   /**
     * ReturnProduct search
     */
    @RequestMapping("/getReturnProductView")
    public void getReturnProductView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getReturnProductView start");
		model.addAttribute("resultDetailList",returnProductService.getReturnProductDetailView(param));
    }
    
    /**
     * ReturnProduct SAVE
     */
    @RequestMapping("/saveReturnProduct")
    public void saveReturnProduct(@RequestParam Map param, Model model, HttpServletRequest request) throws Exception {
    	System.out.println("=======================> saveOut Start");    
    	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	param.put("sesseionId", userInfo.getUserId());
    	model.addAttribute("result",returnProductService.addReturnProduct(param, request));        
    }

   /**
    * ReturnProductDetail Update
    */
    @RequestMapping("/modifyReturnProductDetail")
    public void modifyReturnProductDetail(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> modifyReturnProductDetail Start");
        UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	param.put("sesseionId", userInfo.getUserId());
        model.addAttribute("result", returnProductService.modifyReturnProductDetail(param) );
    }

	/**
     * ReturnProductDetail del
     */
    @RequestMapping("/delReturnProductDetail")
    public void delReturnProductDetail(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> delReturnProductDetail Start");
        model.addAttribute("result", returnProductService.delReturnProductDetail(param) );
    }
}
